package com.shujia.day20.lambda6;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/*
    java内置函数式接口：断言式接口
    只有函数式接口【有且仅有一个抽象方法】才可以被@FunctionalInterface注解所修饰

    @FunctionalInterface
    public interface Predicate<T> {
        boolean test(T t);
    }

 */
public class StaffDemo {
    private static final List<Staff> list = Arrays.asList(
            new Staff("sj1001", "李刚", 18, 35000),
            new Staff("sj1002", "钱志强", 13, 20000),
            new Staff("sj1003", "江川", 24, 50000),
            new Staff("sj1004", "祝帅", 16, 21000),
            new Staff("sj1005", "吴问强", 8, 8000)
    );

    public static void main(String[] args) {
        // 需求1：过滤出薪资大于30000的员工
        List<Staff> staffs1 = filterWithCondition(list, s -> s.getSalary() > 30000);
        for (Staff staff : staffs1) {
            System.out.println(staff);
        }

        System.out.println("--------------------------------");
        // 需求2：过滤出年龄小于18的员工
        //使用lambda表达式改写
        List<Staff> staffs2 = filterWithCondition(list, s -> s.getAge() < 18);

        for (Staff staff : staffs2) {
            System.out.println(staff);
        }


    }


    public static List<Staff> filterWithCondition(List<Staff> list, Predicate<Staff> predicate) {
        ArrayList<Staff> list2 = new ArrayList<>();
        for (Staff staff : list) {
            if (predicate.test(staff)) {
                list2.add(staff);
            }
        }
        return list2;
    }
}
